<template>
  <div class="tenantMenu_main main_container user_container">
    <div v-loading="loading" class="main_main">
      <div class="main_main_container">
        <div class="main_main_left">
          <div class="title">备件信息</div>
          <div class="main_main_left_top">
            <el-tree
              :data="typeListArr"
              :expand-on-click-node="false"
              :default-expand-all="true"
              :highlight-current="true"
              :props="defaultProps"
              @node-click="getSpareInfo"
            />
          </div>
        </div>
        <div class="main_main_right">
          <div class="SearchBar_main main_main_search">
            <div class="main_main_search_item">
              <!-- <a class="button" @click="add">
                新增
              </a> -->
            </div>
            <div class="main_main_search_item">
              <div class="name">
                <el-input v-model="pageData.searchValue" placeholder="请输入" clearable @keyup.enter.native="getList" @clear="getList" />
              </div>
              <el-button style="background:#387bfc;width:30px;" icon="el-icon-search" @click.stop="getList" />
            </div>
          </div>
          <div ref="table_container" style="margin-top:20px" class="table_container">
            <el-table
              ref="table"
              :data="tableData"
              stripe
              border
              :height="tableHeight"
              style="width: 99.9%"
              row-key="menuId"
              :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
              @sort-change="
                (column) => {
                  sortChange(column, this);
                }
              "
              @selection-change="
                (val) => {
                  selectionChange(val, this);
                }
              "
            >
              <el-table-column type="selection" width="70" align="center" />
              <Column
                v-for="(column, index) in defaultTableColumns"
                :key="index"
                :prop="column.prop"
                :is-show="column.isShow"
                :label="column.label"
                :sortable="column.sortable"
                :width="column.width"
              />
            </el-table>
          </div>

          <div class="main_bottom">
            <pagination
              :total="total"
              :page.sync="pageData.pageNum"
              :limit.sync="pageData.pageSize"
              :selected-num="selectionsAll.length"
              @pagination="getList"
            />
          </div>
        </div>
      </div>
    </div>
    <div style="background:white;border-top:1px solid rgba(51, 51, 51, 0.5)" class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button @click.stop="onSubmit">保存</el-button>
    </div>
  </div>
</template>

<script>
import { batchbylist } from '@/api/equipmentManage/equipmentRepair/repairRecord'
import { list as typeList } from '@/api/equipmentManage/sparePart/type'
export default {
  name: 'Index',
  props: {
    layerid: {},
    spareList: {}
  },
  data() {
    return {
      pageId: '选择备件信息',
      loading: true,
      pageData: {
        companyName: undefined,
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc',
        searchValue: ''
      },
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        // { prop: 'code', label: '状态' },
        { prop: 'spareCode', label: '备件编码' },
        { prop: 'spareName', label: '备件名称' },
        { prop: 'spareModel', label: '规格型号' },
        { prop: 'supplierName', label: '备件供应商' },
        { prop: 'spareTypeName', label: '备件类型' },
        { prop: 'numberUnitLabel', label: '备件单位' },
        { prop: 'safeInventory', label: '安全库存' },
        { prop: 'batch', label: '批号' },
        { prop: 'number', label: '库存数量' },
        { prop: 'deviceSpareWarehouseName', label: '仓库' }
      ],
      materialData: [],
      total: 1, // 数据一共有多少条
      mainId: 'batchInventoryId',
      tableHeight: '0',
      tableData: [],
      typeListArr: [],
      rowId: '',
      typeId: '',
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  computed: {
    // 分页器
    selectedNum() {
      return this.$refs.table ? this.$refs.table.selection.length : 0
    }
  },
  mounted() {
    this.getList()
    this.getTypeList()
    this.selectionsAll = this.spareList
    console.log(this.selectionsAll)
    this.$nextTick(() => {
      this.tableHeight = this.$refs.table_container.offsetHeight
    })
  },
  methods: {
    getSpareInfo(item) {
      this.rowId = item.id
      this.selections = []
      this.selectionsAll = []
      this.typeId = item.id
      this.getList()
    },
    getTypeList() {
      typeList(this.pageData).then(res => {
        this.typeListArr.push({ name: '全部', id: '', children: [] })
        this.typeListArr[0].children = res.data
      })
    },
    getList() {
      batchbylist({ ...this.pageData, spareType: this.typeId, status: 1 }).then(res => {
        this.tableData = res.data
        this.total = res.total
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
        })
      })
    },
    refresh() {
      this.pageData.pageNum = 1
      this.getList()
    },
    cancel() { // 取消
      this.$layer.close(this.layerid) // 关闭弹框
    },
    onSubmit() { // 保存
      this.$parent.addPanyCallBack(this.selectionsAll)// 给父组件传值的方法
      this.$layer.close(this.layerid) // 关闭弹框
    }
  }
}
</script>

<style lang="less" scoped>
.main_main {
  height: calc(100% - 50px);
}
.main_main_container {
  width: 100%;
  height: 100%;
  display: flex;

  .main_main_left {
    width: 250px;
    height: calc(100% - 20px);
    padding-right: 20px;
    .title {
      padding: 10px 0;
      border-bottom: 1px solid #387bfc;
      margin-bottom: 10px;
    }
    .main_main_left_top {
      width: 100%;
      height: calc(100% - 80px);
      overflow-y: auto;
      .el-tree {
        background-color: transparent;
      }
      /deep/ .el-tree-node__content:hover {
        background-color: rgba(56, 123, 252, 0.5) !important;
        color: white;
      }
    }
    .main_main_left_bottom {
      width: 100%;
      .main_main_left_bottom_item {
        background-color: rgba(74, 77, 82, 0.2);
        height: 40px;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        padding-left: 24px;
      }
      .main_main_left_bottom_item:last-child {
        border-top: 1px solid white;
      }
      .main_main_left_bottom_item:hover {
        background-color: rgba(56, 123, 252, 0.5);
        color: white;
      }
      .active {
        .main_main_left_bottom_item {
          background-color: rgba(56, 123, 252, 1);
          color: white;
        }
      }
    }
  }
  .main_main_right {
    width: calc(100% - 250px);
    height: 100%;
    display: flex;
    flex-direction: column;
    border-left: 1px solid rgba(51, 51, 51, 0.5);

  }
}
.button{
    width: 100px;
    height: 30px;
    color: #fff;
    background-color: #387bfc;
    border-radius: 4px;
    text-align: center;
    line-height: 30px;
}
</style>
